import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class Test:
    def login(self,username,password,driver):
        driver.find_element(By.XPATH,'//*[@id="username"]').send_keys(username)
        driver.find_element(By.XPATH,'//*[@id="password"]').send_keys(password)
        driver.find_element(By.XPATH,'//*[@id="login_btn"]').click()
        time.sleep(5)
        if driver.title=='个人理财系统':
            print('用户登陆成功')
        else:
            driver.save_screenshot('userlogin.png')
    def register(self,username,password,driver):
        driver.find_element(By.XPATH,'/html/body/div/div/div/div[2]/a').click()
        time.sleep(5)
        driver.find_element(By.XPATH,'//*[@id="username"]').send_keys(username)
        registerusername=driver.find_element(By.XPATH,'//*[@id="username"]').get_attribute('value')##这里也可以用beautifulsoup库
        driver.find_element(By.XPATH, '//*[@id="password"]').click()
        temptext='用户'+registerusername+'已存在'
        systemtext=driver.find_element(By.XPATH,'/html/body/div/div/div/form/div[1]/span[1]').text
        if(temptext==systemtext):
            print(f"用户{registerusername}已存在")
        else:
            driver.find_element(By.XPATH,'//*[@id="password"]').send_keys(password)
            driver.find_element(By.XPATH,'//*[@id="repassword"]').send_keys(password) ##未重复确认密码验证
            time.sleep(5)
            driver.find_element(By.XPATH,'//*[@id="login_btn"]').click()
            time.sleep(5)
    def lingmoney(self,username,password,account,driver):
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
        driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()
        time.sleep(5)
        driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').click()
        driver.find_element(By.XPATH, '//html/body/div[3]/div[2]/input').send_keys(account)
        time.sleep(5)
        driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(5)
        temptext=driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        if(temptext=='买入失败!'):
            driver.save_screenshot('买入失败.png')
        else:
            print('买入支付宝零钱理财成功')##密码无论如何输入都无法成功解决方法将原密码登陆值获取
    def addcard(self,blank,driver):
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(5)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()##点击到银行卡管理
        driver.find_element(By.XPATH,'//*[@id="bankCard_add_modal_btn"]').click()
        time.sleep(5)
        driver.find_element(By.XPATH,'//*[@id="cardBank_add_input"]').click()
        driver.find_element(By.XPATH,'//*[@id="cardBank_add_input"]').send_keys(blank)
        time.sleep(2)
        driver.find_element(By.XPATH,'//label[@class="lyear-radio radio-inline"][2]').click()
        time.sleep(5)
    def addcarderror(self,blank,blankcard,driver):
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(5)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
        time.sleep(5)
        driver.find_element(By.XPATH,'//*[@id="bankCard_add_modal_btn"]').click()
        time.sleep(5)
        driver.find_element(By.XPATH,'//*[@id="cardBank_add_input"]').click()
        driver.find_element(By.XPATH,'//*[@id="cardBank_add_input"]').send_keys(blank)
        driver.find_element(By.XPATH,'//*[@id="cardNum_add_input"]').click()
        driver.find_element(By.XPATH, '//*[@id="cardNum_add_input"]').send_keys(blankcard)
        driver.find_element(By.XPATH, '//*[@id="bankCard_save_btn"]').click()
        time.sleep(4)
        temptext=driver.find_element(By.XPATH,'//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[3]/div[1]/span').text
        assert temptext=='银行卡号不合法'
        driver.save_screenshot('卡号异常新增.png')
        time.sleep(5)
    def addsamecard(self,blank,blankcard,driver):
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(5)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
        time.sleep(5)
        driver.find_element(By.XPATH, '//*[@id="bankCard_add_modal_btn"]').click()
        time.sleep(5)
        driver.find_element(By.XPATH, '//*[@id="cardBank_add_input"]').click()
        driver.find_element(By.XPATH, '//*[@id="cardBank_add_input"]').send_keys(blank)
        driver.find_element(By.XPATH, '//*[@id="cardNum_add_input"]').click()
        driver.find_element(By.XPATH, '//*[@id="cardNum_add_input"]').send_keys(blankcard)
        cardid=driver.find_element(By.XPATH, '//*[@id="cardNum_add_input"]').get_attribute('value')
        elements=driver.find_elements(By.XPATH, '//div[@class="card-body"]/h4')
        if len(elements)==1:
            for i in elements:
                print(i.text)
        else:
            dict={}
            for i in elements:
                dict[i.text[3:21]]=i.text
            if(dict[cardid]):
                print('有相同卡!')
                driver.save_screenshot('新增卡相同.png')
    def deletecard(self,driver):
        driver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(5)
        driver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
        time.sleep(5)
        h4numbers=driver.find_elements(By.XPATH,'//*[@class="card-body"]/h4')
        if(len(h4numbers)==1):
            print('很抱歉！您还没有添加银行卡')
        else:
            beforetext=driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[2]').get_attribute('delete-id')
            print(beforetext)
            driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[2]').click()
            time.sleep(5)
            driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
            driver.refresh()
            aftertext=driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[2]').get_attribute('delete-id')
            print(aftertext)
            assert beforetext!=aftertext
            print('删除成功！')
    def deletecardbeetween(self,driver):
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(5)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
        time.sleep(5)
        h4numbers = driver.find_elements(By.XPATH, '//*[@class="card-body"]/h4')
        if (len(h4numbers) == 1):
            print('很抱歉！您还没有添加银行卡')
        else:
            beforetext = driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[2]').get_attribute('delete-id')
            driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[2]').click()
            time.sleep(5)
            driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[2]').click()
            driver.refresh()
            aftertext = driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[2]').get_attribute('delete-id')
            assert beforetext == aftertext
            print('取消成功！')
    def Onlineloansadd(self,money,day,driver):
        ##//tr/td[2] 5条钱
        ##//button[@class="btn btn-default"][2]/span  1/3页
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(4)
        driver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[2]/a').click()
        time.sleep(5)
        driver.find_element(By.XPATH,'//*[@id="loan_add_modal_btn"]').click()
        driver.find_element(By.XPATH,'//*[@id="amount"]').click()
        driver.find_element(By.XPATH, '//*[@id="amount"]').send_keys(money)
        time.sleep(5)
        driver.find_element(By.XPATH,'//*[@id="term"]').click()
        driver.find_element(By.XPATH, '//*[@id="term"]').send_keys(day)
        temptext=driver.find_element(By.XPATH, '//*[@id="term"]').get_attribute('value')
        if(int(temptext)>365):
            print("请重新输入")
        else:
            print('1')
        driver.find_element(By.XPATH,'//*[@id="submit"]').click()
        time.sleep(5)
        driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(5)



